package de.ifi.lmu.snitzelhunt.participants;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.telephony.TelephonyManager;
import android.util.Log;
import de.ifi.lmu.snitzelhunt.basics.Route;
import de.ifi.lmu.snitzelhunt.basics.User;
import de.ifi.lmu.snitzelhunt.util.ImageServerUtility;
import de.ifi.lmu.snitzelhunt.util.RankingServerUtility;
import de.ifi.lmu.snitzelhunt.util.RouteServerUtility;
import de.ifi.lmu.snitzelhunt.util.basics.Ranking;
import de.ifi.lmu.snitzelhunt.util.basics.RouteInfo;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_Image_DownloadResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_Ranking_CreateResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_Ranking_ReadResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_RouteInfo_ReadforUserResponse;
import de.ifi.lmu.snitzelhunt.util.responses.AsyncServer_Route_ReadResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_Image_DownloadResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_Ranking_CreateResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_Ranking_ReadResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_RouteInfo_ReadforUserResponse;
import de.ifi.lmu.snitzelhunt.util.responses.Server_Route_ReadResponse;

/**
 * 
 * @author Sebastian Siepe
 *
 */
public class GetRouteData implements AsyncServer_RouteInfo_ReadforUserResponse, AsyncServer_Route_ReadResponse, AsyncServer_Ranking_ReadResponse, AsyncServer_Ranking_CreateResponse, AsyncServer_Image_DownloadResponse  
{
	private boolean debugMode = false;
	ArrayList<RouteInfo> availableRoutes = null;
	Route route = new Route();
	private Context context;
	public String OwnNumber = "";
	private ArrayList<Ranking> rankingList = new ArrayList<Ranking>();
	private Bitmap image = null;

	public void setOwnNumber(String ownNumber) 
	{
		OwnNumber = ownNumber;
	}

	public String getCellNumber(Context context) {
		String output = "";
		// Receive Own TelephoneNumer:
		TelephonyManager tMgr = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String mPhoneNumber = tMgr.getLine1Number();

		if (mPhoneNumber != null) {
			if (mPhoneNumber.startsWith("01")) 
			{
				output = mPhoneNumber.replaceFirst("0", "+49");
			} else {
				output = mPhoneNumber;
			}
		} else {
			output = "Eigene Nummer konnte nicht gelesen werden. SIM Karte im Handy?";
		}
		Log.i("meineNummer","ist "+output);
		
		setOwnNumber(output);
		return output;
	}

	//Server Requests
	public void serverRequest_routeInfo(Context context) {
		this.context = context;
		//On Real Device:
		User user = new User("User", OwnNumber);
		if(debugMode)
		{
			user = new User("Ich", "+4917634906238");			
		}
		new RouteServerUtility().readRouteInfo_forUser(user, this);
	}
	
	public void serverRequest_route(long GlobalID)
	{
		this.route.setGlobal_routeId(GlobalID);
		new RouteServerUtility().readRoute(route.getGlobal_routeId(), this);
	}
	
	public void serverRequest_rankingIndex(Long globalID, Ranking ranking)
	{
		new RankingServerUtility().readRanking(globalID, this);
	}
	
	public void serverRequest_setOwnRanking(Ranking ranking)
	{
		new RankingServerUtility().createRanking(ranking, this);
	}
	
	public void serverRequest_Image(Long globalImageID)
	{
		new ImageServerUtility().downloadImage(globalImageID, this);
	}
	
	public Route getRoute()
	{
		return this.route;
	}

	public ArrayList<RouteInfo> getRouteInfoArray() 
	{
		System.out.println(availableRoutes.toString());
		return availableRoutes;
	}
	
	public ArrayList<Ranking> getRanking()
	{
		return this.rankingList;
	}
	
	public Bitmap getBitmap()
	{
		return this.image;
	}

	@Override
	public void processServer_RouteInfo_ReadforUser(Server_RouteInfo_ReadforUserResponse response) 
	{
		Log.i("snitzelhunt util ", "downloaded RouteInfo " + response.getHttpStatus());

		if (response.getHttpStatus() == 200) 
		{
			if (response.getRinfo().getInfo() != null) 
			{
				Log.i("found routes:",""+response.getRinfo().getInfo().size());
				availableRoutes = response.getRinfo().getInfo();
				Intent progIn = new Intent("AVAILABLEROUTES_LOADED");
				context.sendBroadcast(progIn);
				System.out.println("Broadcast sent!");
			} 
			else 
			{
				Log.i("snitzelhunt util ", "no Routes found");
			}
		} 
		else 
		{
			Log.i("snitzelhunt util ", "RInfoContainer null ");
		}
	}

	@Override
	public void processServer_Route_Read(Server_Route_ReadResponse response) 
	{
		if (response.getHttpStatus() == 200) 
		{
			if (response.getRecvRoute() != null) 
			{

				route  = response.getRecvRoute();
				Intent progIn = new Intent("START_HUNT");
				context.sendBroadcast(progIn);
			} 
			else 
			{
				Log.i("snitzelhunt util ", "no Routes found");
			}
		} 
		else 
		{
			Log.i("snitzelhunt util ", "RInfoContainer null ");
		}
		
	}

	@Override
	public void processServer_Ranking_ReadResponse(Server_Ranking_ReadResponse response) 
	{
		if (response.getHttpStatus() == 200) 
		{
			if (response.getRankCont() != null) 
			{
				rankingList = response.getRankCont().getRanking();
				rankingList.size();
				Intent progIn = new Intent("GOT_RANKING");
				context.sendBroadcast(progIn);
			} 
			else 
			{
				Log.i("snitzelhunt util ", "no Routes found");
			}
		} 
		else 
		{
			Log.i("snitzelhunt util ", "RInfoContainer null ");
		}
		
	}

	@Override
	public void processServer_Ranking_Create(Server_Ranking_CreateResponse response) 
	{
		if (response.getHttpStatus() == 200) 
		{
				Intent progIn = new Intent("SEND_RANKING");
				context.sendBroadcast(progIn);
		} 
		else 
		{
			Log.i("snitzelhunt util ", "RInfoContainer null ");
		}
		
	}

	@Override
	public void processServer_Image_DownloadResponse(Server_Image_DownloadResponse response) 
	{
		if (response.getHttpStatus() == 200) 
		{
			if (response.getBmp() != null) 
			{
				image = response.getBmp();
				Intent progIn = new Intent("GOT_IMAGE");
				context.sendBroadcast(progIn);
			} 
			else 
			{
				Log.i("snitzelhunt util ", "no Routes found");
			}
		} 
		else 
		{
			Log.i("snitzelhunt util ", "RInfoContainer null ");
		}
		
	}
	
	
}
